package util.pub;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//import com.sun.image.codec.jpeg.ImageFormatException;
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.imageio.ImageIO;

/**
 * 文件压缩
 *
 */
public class ZoomPic {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		String srcPath = "http://localhost:9000/oainfo/publish/20101028/";
//		String srcFileName = "201010281552181872613501.jpg";
//		String newFileName = "201010281552181872613502.jpg";
//		zoomPic(srcPath,srcFileName,newFileName);
	}
	
	public static void zoomPic(String srcPath,String srcFileName,String newFileName, int width, int height) {
		long startTime = System.nanoTime();
		//压缩源文�?
		String srcFile = srcPath + srcFileName;
		//构建源文件对�?
		java.io.File file = new java.io.File(srcFile); 
		//压缩文件路径
		//String desPath="c:/des/";
		//压缩文件名：lit_ + 源文件名
		//String fileZoomName = "lit_" + fileName;
		//压缩文件类型和源文件类型�?��
		String fileType = srcFile.substring(srcFile.indexOf("."));
		String desFile = srcPath + newFileName;
		
		Image src = null;
		try {
			//构建image对象
			src = javax.imageio.ImageIO.read(file);
		} catch (IOException e1) {
			System.out.println("没有找到压缩源文件！");
		} 
		//float tagsize = 300;
		//得到源图�?
		int old_w = src.getWidth(null); 
		//得到源图�?
		int old_h = src.getHeight(null);
		/*int new_w = 0;
		int new_h = 0; 
		float tempdouble;
		if (old_w > old_h) {
			tempdouble = old_w / tagsize;
		} else {
			tempdouble = old_h / tagsize;
		}*/
		//计算新图宽度
		int new_w = (int) Math.round(width);
		//计算新图高度
		int new_h = (int) Math.round(height);
		BufferedImage tag = new BufferedImage(new_w, new_h,
				BufferedImage.TYPE_INT_RGB);
		//绘制压缩文件
		tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); 
		FileOutputStream newimage = null;
		try {
			newimage = new FileOutputStream(desFile);
//			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
//			encoder.encode(tag);
			ImageIO.write(tag, "JPEG", newimage);
			newimage.close();
		} catch (FileNotFoundException e) {
			System.out.println("文件路径错误�");
//		} catch (ImageFormatException e) {
//			System.out.println("文件格式不正确！");
		} catch (IOException e) {
			System.out.println("文件压缩出错�");
		} 
		long endTime = System.nanoTime();
		System.out.println("执行时间：" + String.valueOf(endTime - startTime)	+ " ns");
	}
}
